//
//  MainViewController.swift
//  weibotest
//
//  Created by 冷冷清清 on 16/7/1.
//  Copyright © 2016年 tgl.cn. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //添加子控制器
        addChildViewController()

        
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        tabBar.addSubview(composeButton)
        let rect = composeButton.frame.size
        let width = tabBar.bounds.width/CGFloat(childViewControllers.count)
        composeButton.frame = CGRectMake(2*width, 0,width, rect.height)
        
        
        
    }
    // MARK:-内部控制方法
    //添加子控制器方法
    func addChildViewController()
    {   //读取JSON 数据
     guard  let pathFile = NSBundle.mainBundle().pathForResource("MainVCSettings.json", ofType: nil)
        else{
            NJLog("获取json失败")
        return
        }
     guard let data = NSData(contentsOfFile: pathFile)
        else{
            NJLog("加载json失败")
        return
        }
        
    do {//swift和oc不同，oc中一般情况如果不发生错误会给传入的指针赋值，而在swift中使用的是异常处理机制
        let obj = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as! [[String:AnyObject]]
        
        for dict in obj
        {
            let title = dict["title"] as? String
            let vcName = dict["vcName"] as? String
            let imageName = dict["imageName"] as? String
            
        addTabBar(vcName,title: title,imageName: imageName)
            
            
        }
        
        }catch{
        //只要try对应的方法发生了异常，就会被执行catch中的代码
            addTabBar("HomeTableViewController",title: "首页",imageName: "tabbar_home")
            addTabBar("MessageTableViewController", title: "消息", imageName:"tabbar_message_center")
            addTabBar("NullViewController", title: "", imageName: "")
            addTabBar("DiscoverTableViewController", title: "发现", imageName: "tabbar_discover")
            addTabBar("ProfileTableViewController", title: "我", imageName: "tabbar_profile")
        
        }
        /*
        addTabBar(HomeTableViewController(),title: "首页",imageName: "tabbar_home")
        addTabBar(MessageTableViewController(), title: "消息", imageName:"tabbar_message_center")
        addTabBar(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addTabBar(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
        */

        
        //修改tabar字体颜色
        tabBar.tintColor = UIColor.orangeColor()
    }
  
    //子控制器属性设置
    func addTabBar(childViewController:String?,title:String?,imageName:String?)
    {
       //获取命名空间
        
        guard let name = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as? String else
        {
            return
        }
        var cls:AnyClass? = nil
        if let vcName = childViewController
        {
            cls = NSClassFromString(name + "." + vcName)
        }
        
        guard let typeCls = cls as? UITableViewController.Type else
        {
        return
        }
        let childController = typeCls.init()
//        NJLog(childController)
        
//        NJLog(childViewController)
        
        childController.title = title
        if let ivname = imageName
        {
            childController.tabBarItem.image = UIImage(named: ivname)
            childController.tabBarItem.selectedImage = UIImage(named: ivname  + "_highlighted")
        }
        

        let nav = UINavigationController(rootViewController: childController)
        
        addChildViewController(nav)
        
        
    }
  @objc private func btnClick()
    {
        NJLog("++++")
        
    }
    //MARK:添加+号按钮_懒加载
    lazy var composeButton: UIButton = {
    () -> UIButton
        in
         /*//写进UIButton一个扩展
        let btn = UIButton()
       
        btn.setImage(UIImage(named:"tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        */
        let btn = UIButton(imageName:"tabbar_compose_icon_add",backgroundName:"tabbar_compose_button")
        btn.addTarget(self, action: #selector(MainViewController.btnClick), forControlEvents: UIControlEvents.TouchUpInside)
            btn.sizeToFit()
    
    return btn
    }()
    



}
